package com.shan;

import com.shan.dao.BrandMapper;
import com.shan.pojo.Brand;
import com.shan.pojo.SqlSessionUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SelectDemo {
    public static void main(String[] args) throws IOException {

        selectAll();
        //按照id传值
        //selectDan(1);

        //一个一个的传值
        //selectDuo1();

        //对象传值
        //selectDuo2();

        //map传值
        //selectDuo3();

    }


    //map传值
    private static void selectDuo3() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        String brandName = "%华为%";
        String companyName = "%华为%";
        int status = 1;

        Map map = new HashMap();
        map.put("brandName", brandName);
        map.put("companyName", companyName);

        List<Brand> brands = mapper.selectDuo3(map);

        System.out.println(brands);

        sqlSession.close();
    }


    //包装成对象传值
    private static void selectDuo2() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        String brandName = "%华为%";
        String companyName = "%华为%";
        int status = 1;

        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);

        List<Brand> brands = mapper.selectDuo2(brand);

        System.out.println(brands);

        sqlSession.close();
    }

    //单个传值
    private static void selectDuo1() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        String brandName = "%华为%";
        String companyName = "%华为%";
        int status = 1;

        List<Brand> brands = mapper.selectDuo1(brandName, companyName, status);

        System.out.println(brands);
        sqlSession.close();
    }

    private static void selectDan(int id) throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = mapper.selectDan(id);
        System.out.println(brand);

        sqlSession.close();
    }

    private static void selectAll() throws IOException {

        SqlSession sqlSession = SqlSessionUtil.getSqlSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();
        System.out.println(brands);

        sqlSession.close();
    }


}
